package com.lagou.edu.controller;

import com.lagou.edu.dao.ResumeDao;
import com.lagou.edu.pojo.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Controller
@RequestMapping(value = "/resume")
public class ResumeController {

    @Autowired
    private ResumeDao resumeDao;

    @RequestMapping(value = "/login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response){
        Cookie cookie = new Cookie("username","admin");
        cookie.setMaxAge(60*60*24);
        response.addCookie(cookie);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/jsp/list");
        return mv;
    }

    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(){
        List<Resume> all = resumeDao.findAll();
        Map<String,Object> map = new HashMap<>();
        map.put("list",all);
        return all;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public Object del(@RequestBody Resume resume){
        resumeDao.delete(resume);
        Map<String,Object> map = new HashMap<>();
        map.put("status",200);
        return map;
    }

    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(@RequestBody Resume resume){
        resumeDao.save(resume);
        Map<String,Object> map = new HashMap<>();
        map.put("status",200);
        return map;
    }

}
